package org.juggers.server;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * Application configuration
 * @author mimah
 *
 */
public class Configuration {
	
	private String reCaptchaKey;
	private String gCalendarSession;
	
	public Configuration() {
		super();
	}
		
	/**
	 * @param reCaptchaKey the reCaptchaKey to set
	 */
	public void setReCaptchaKey(String reCaptchaKey) {
		this.reCaptchaKey = reCaptchaKey;
	}

	/**
	 * @param gCalendarSession the gCalendarSession to set
	 */
	public void setgCalendarSession(String gCalendarSession) {
		this.gCalendarSession = gCalendarSession;
	}

	/**
	 * @return the reCaptchaKey
	 */
	public String getReCaptchaKey() {
		return reCaptchaKey;
	}

	/**
	 * @return the gCalendarSession
	 */
	public String getgCalendarSession() {
		return gCalendarSession;
	}
	
	private static DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
	
	private static Key key = KeyFactory.createKey( "config", 1L );
	
	private static Configuration instance;
	
	public static Configuration getInstance() {
		if ( instance == null ) {
			try {
				instance = new Configuration( datastoreService.get( key ) );
			}
			catch ( EntityNotFoundException ex ) {
				
			}
		}
		return instance;
	}
	
	public void save() {
		Entity entity = new Entity( key );
		entity.setProperty( "reCaptchaKey", reCaptchaKey );
		entity.setProperty( "gCalendarSession", gCalendarSession  );
		datastoreService.put( entity );
	}
	
	private Configuration( Entity entity ) {
		this.reCaptchaKey = (String) entity.getProperty( "reCaptchaKey" );
		this.gCalendarSession = (String) entity.getProperty( "gCalendarSession" );
	}
}
